package com.tansuo365.test1.service.kucunservice.kucunimpl;

import com.tansuo365.test1.bean.kucunbean.*;
import com.tansuo365.test1.bean.otherbean.HuiShouZhixing;
import com.tansuo365.test1.mapper.kucunmapper.ChukuDetailsMapper;
import com.tansuo365.test1.mapper.kucunmapper.ChukuZhixingMapper;
import com.tansuo365.test1.mapper.kucunmapper.ChukuZongMapper;
import com.tansuo365.test1.mapper.kucunmapper.TuikuDetailsMapper;
import com.tansuo365.test1.mapper.othermapper.HuiShouZhixingMapper;
import com.tansuo365.test1.service.kucunservice.ChukuZhixingService;
import org.springframework.stereotype.Service;
import com.tansuo365.test1.util.StringUtil;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ChukuZhixingServiceImpl implements ChukuZhixingService {

    @Resource
    private ChukuZhixingMapper chukuZhixingMapper;
    @Resource
    private ChukuZongMapper chukuZongMapper;
    @Resource
    private ChukuDetailsMapper chukuDetailsMapper;
    @Resource
    private TuikuDetailsMapper tuikuDetailsMapper;
    @Resource
    private HuiShouZhixingMapper huiShouZhixingMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return chukuZhixingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(ChukuZhixing chukuZhixing) {
        return chukuZhixingMapper.insertSelective(chukuZhixing);
    }

    /**
     * 退库关联出库单
     * 只有bindSave
     *
     * @param tuikuDetailsList
     * @param tuikuZong
     * @return
     */
    @Override
    public int saveTuikuBindChuku(List<TuikuDetails> tuikuDetailsList, TuikuZong tuikuZong) {
//        int isBindSave = 0;
        String chukudanhaoBind = "";
        int i = 0;
        int insertResultCode = 0;
        for (TuikuDetails tuikuDetails : tuikuDetailsList) {
            //如果退库数据详细有对应出库单号(关联操作)
            String duiyingchukudanhao = tuikuDetails.getDuiyingchukudanhao();
            String mingchengguige = tuikuDetails.getMingchengguige();
            if (StringUtil.isNotEmpty(duiyingchukudanhao)) {
//                isBindSave ++;
                ChukuZhixing chukuZhixingData = chukuZhixingMapper.listChukuZhixingByDanhaoAndMingcheng(
                        duiyingchukudanhao, mingchengguige);
                HuiShouZhixing huiShouZhixingData = huiShouZhixingMapper.listHuishouZhixingByDanhaoAndMingcheng(
                        duiyingchukudanhao, mingchengguige);
                if (huiShouZhixingData != null) {
                    HuiShouZhixing huiShouZhixing = new HuiShouZhixing();
                    //设置参数
                    huiShouZhixing.setDuiyingchukudanhao(huiShouZhixingData.getDuiyingchukudanhao());
                    huiShouZhixing.setMingchengguige(huiShouZhixingData.getMingchengguige());
                    huiShouZhixing.setDanwei(huiShouZhixingData.getDanwei());
                    Double chukuhuishoushuliang = huiShouZhixingData.getChukushuliang(); //出库数量(可回收的)
                    Double tuikushuliang = tuikuDetails.getTuihuishuliang();
                    Double yihuishoushuliang = huiShouZhixingData.getHuishoushuliang();
                    //未回收量= 出库单中绑定的这条物料是可回收的 出库多少 - 用户填入的已回收量
                    Double tuikushuliang_truely = (tuikushuliang + huiShouZhixingData.getTuikushuliang());
                    Double weihuishouliang =
                            chukuhuishoushuliang - tuikushuliang_truely - yihuishoushuliang;
                    huiShouZhixing.setChukushuliang(chukuhuishoushuliang);
                    huiShouZhixing.setHuishoushuliang(0.0);
                    huiShouZhixing.setTuikushuliang(tuikushuliang_truely);
                    huiShouZhixing.setWeihuishoushuliang(weihuishouliang);
//            huiShouZhixing.setChukushijian(huiShouZong.get);
                    String huishouzhuangtai = "";
                    double preErrorKey = 1e-6; //1*10的-6次方即0.000001
                    double key1 = 0.0000001d;
                    double key2 = 0d;
                    if ((chukuhuishoushuliang - tuikushuliang_truely < preErrorKey) && (weihuishouliang < preErrorKey)) {
                        huishouzhuangtai = "全部退库";
                    } else if (Math.abs(chukuhuishoushuliang - tuikushuliang_truely - yihuishoushuliang) < preErrorKey && (weihuishouliang == 0)) {
                        huishouzhuangtai = "全部回收";
                    } else if ((yihuishoushuliang + tuikushuliang_truely == 0) && (chukuhuishoushuliang != 0)) {
                        huishouzhuangtai = "未回收";
                    } else if (((tuikushuliang_truely + yihuishoushuliang) < chukuhuishoushuliang) && yihuishoushuliang > 0) {
                        huishouzhuangtai = "部分回收";
                    } else if (((tuikushuliang_truely + yihuishoushuliang) < chukuhuishoushuliang) && tuikushuliang_truely > 0) {
                        huishouzhuangtai = "部分退库";
                    } else if ((tuikushuliang_truely + yihuishoushuliang) > chukuhuishoushuliang) {
                        huishouzhuangtai = "超量回收";
                    }
                    huiShouZhixing.setHuishouzhuangtai(huishouzhuangtai);
                    huiShouZhixing.setId(huiShouZhixingData.getId());
                    int i1 = huiShouZhixingMapper.updateZhixingChangeHuishouWeiHuishouAndZhuangtai(huiShouZhixing);
                }

                ChukuZhixing chukuZhixing = new ChukuZhixing();
                chukuZhixing.setDuiyingchukudanhao(tuikuDetails.getDuiyingchukudanhao());
                Double chukushuliang = tuikuDetails.getChukushuliang(); //出库数量
                Double tuihuishuliang = tuikuDetails.getTuihuishuliang(); //退库数量
                Double weituihuiliang = chukushuliang - tuihuishuliang;
                chukuZhixing.setChukushuliang(chukushuliang);
                chukuZhixing.setTuikushuliang(tuihuishuliang);
                chukuZhixing.setWeituihuiliang(weituihuiliang);
                chukuZhixing.setWuliaobianma(tuikuDetails.getWuliaobianma());
                chukuZhixing.setMingchengguige(tuikuDetails.getMingchengguige());
                String chukutuikuzhuangtai = "";
                double preErrorKey = 1e-6;
                if (chukuZhixingData == null) {
                    i++;
                    if (Math.abs(tuihuishuliang - chukushuliang) < preErrorKey && (weituihuiliang == 0)) {
                        chukutuikuzhuangtai = "全部退回";
                    } else if ((tuihuishuliang == 0) && (chukushuliang != 0)) {
                        chukutuikuzhuangtai = "未退回";
                    } else if ((tuihuishuliang < chukushuliang) && tuihuishuliang > 0) {
                        chukutuikuzhuangtai = "部分退回";
                    } else if (tuihuishuliang > chukushuliang) {
                        chukutuikuzhuangtai = "超量退回";
                    }
                    chukuZhixing.setChukutuikuzhuangtai(chukutuikuzhuangtai);
                    int i1 = chukuZhixingMapper.insertSelective(chukuZhixing);
                    insertResultCode += i1;
                } else {
                    i++;
                    if (Math.abs(((tuihuishuliang) + chukuZhixingData.getTuikushuliang()) - (chukushuliang)) < preErrorKey && (chukuZhixingData.getChukushuliang()) > 0) {
                        chukutuikuzhuangtai = "全部退回";
                    } else if ((tuihuishuliang + chukuZhixingData.getTuikushuliang() == 0) && (chukushuliang != 0)) {
                        chukutuikuzhuangtai = "未退回";
                    } else if ((tuihuishuliang + chukuZhixingData.getTuikushuliang() < chukushuliang) && tuihuishuliang > 0) {
                        chukutuikuzhuangtai = "部分退回";
                    } else if (tuihuishuliang + chukuZhixingData.getTuikushuliang() > chukushuliang) {
                        chukutuikuzhuangtai = "超量退回";
                    }
                    chukuZhixing.setChukutuikuzhuangtai(chukutuikuzhuangtai);
                    chukuZhixing.setId(chukuZhixingData.getId());
                    int i1 = chukuZhixingMapper.updateZhixingChangeTuikuWeiTuikuAndZhuangtai(chukuZhixing);
                    insertResultCode += i1;
                }
            } else {
                i++;
                insertResultCode++;
            }
        }
        if (insertResultCode == i) {
            return i;
        } else if ((insertResultCode != i) && (insertResultCode > 0 && i > 0)) {
            return -1;
        } else if (insertResultCode == 0) {
            return 0;
        } else {
            return -2;
        }
    }



    @Override
    @Transactional
    public int deleteChukuBindZhixing(Integer[] ids) {
        int delCode1= 0;
        int delCode2= 0;
        for (Integer id : ids) {
            List<ChukuDetails> chukuDetailsList = chukuDetailsMapper.listChukuDetailsByZongId(id);
            ChukuZong chukuZong = chukuZongMapper.selectByPrimaryKey(id);
            for (ChukuDetails chukuDetails : chukuDetailsList) {

                ChukuZhixing chukuZhixing =
                        chukuZhixingMapper.listChukuZhixingByDanhaoAndMingcheng(
                                chukuZong.getWuliaochukudanhao(), chukuDetails.getMingchengguige());
                HuiShouZhixing tuikulikehuishou =
                        huiShouZhixingMapper.getHuishouZhixingByCkdhAndName(
                                chukuZong.getWuliaochukudanhao(), chukuDetails.getMingchengguige());
                //既然没有出库,回收执行这条数据就没有意义,予以直接删除才为上策
                if (tuikulikehuishou != null) {
                    delCode1 = huiShouZhixingMapper.deleteByPrimaryKey(tuikulikehuishou.getId());
                }

                //既然没有出库,出库执行这条数据就没有意义.予以直接删除才为上策
                if (chukuZhixing != null) {
                    delCode2 = chukuZhixingMapper.deleteByPrimaryKey(chukuZhixing.getId());
                }
            }
        }
        //返回出库执行code是肯定要有的,出库执行里记录的本就是出库为主的数据对比
        if (delCode2 > 0) {
            return delCode2;
        } else {
            return 0;
        }
    }

    @Override
    @Transactional
    public int saveChukuToZhixing(List<ChukuDetails> chukuDetailsList, ChukuZong chukuZong) {
        int i = 0;
        int insertResultCode = 0;
        for (ChukuDetails chukuDetails : chukuDetailsList) {
            i++;
            ChukuZhixing chukuZhixing = new ChukuZhixing();
            chukuZhixing.setDuiyingchukudanhao(chukuZong.getWuliaochukudanhao());
            chukuZhixing.setXiangmumingcheng(chukuZong.getXiangmumingcheng());
            chukuZhixing.setWuliaobianma(chukuDetails.getWuliaobianma());
            chukuZhixing.setMingchengguige(chukuDetails.getMingchengguige());
            chukuZhixing.setChukushuliang(chukuDetails.getChukushuliang());
            chukuZhixing.setTuikushuliang(0.0);
            chukuZhixing.setWeituihuiliang(chukuDetails.getChukushuliang());

            //新的字段
            chukuZhixing.setChukukufang(chukuDetails.getChukukufang());
            chukuZhixing.setBeizhu(chukuDetails.getBeizhu());
            chukuZhixing.setDanwei(chukuDetails.getDanwei());
            chukuZhixing.setDalei(chukuDetails.getDalei());
            chukuZhixing.setLingliaodanwei(chukuZong.getLingliaodanwei());
            chukuZhixing.setLingliaoren(chukuZong.getLingliaoren());
            chukuZhixing.setFaliaoren(chukuZong.getFaliaoren());

            String chukutuikuzhuangtai = "未退回";
            chukuZhixing.setChukutuikuzhuangtai(chukutuikuzhuangtai);
            int i1 = chukuZhixingMapper.insertSelective(chukuZhixing);
            insertResultCode += i1;
        }
        if (insertResultCode == i) {
            return i;
        } else if ((insertResultCode != i) && (insertResultCode > 0 && i > 0)) {
            return -1;
        } else if (insertResultCode == 0) {
            return 0;
        } else {
            return -2;
        }
    }

    @Override
    public ChukuZhixing selectByPrimaryKey(Integer id) {
        return chukuZhixingMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ChukuZhixing chukuZhixing) {
        return chukuZhixingMapper.updateByPrimaryKeySelective(chukuZhixing);
    }

    @Override
    public List<ChukuZhixing> listChukuZhixingSelective(ChukuZhixing chukuZhixing) {
        return chukuZhixingMapper.listChukuZhixingSelective(chukuZhixing);
    }

    @Override
    public int deleteByIdArr(Integer[] ids) {
        return chukuZhixingMapper.deleteByIdArr(ids);
    }

    @Override
    public int deleteByChukudans(List<String> chukudanhaos) {
        return chukuZhixingMapper.deleteByChukudans(chukudanhaos);
    }
}
